<?php

namespace App\Http\Validate\Kms\Clients;

use App\Http\Validate\AbstractBaseValidate;
use App\Models\Kms\App\ClientsModel;
use App\Models\Kms\App\ClientsVersionModel;
use App\Models\Kms\App\ClientsVersionReleaseHistoryModel;
use Illuminate\Support\MessageBag;
use Illuminate\Validation\Rule;
use Illuminate\Validation\ValidationException;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;

class ClientsVersionValidate extends AbstractBaseValidate
{
    function getRules(): array
    {
        // TODO: Implement getRules() method.
        return [];
    }

    function getAttributes(): array
    {
        // TODO: Implement getAttributes() method.
        return [];
    }

    function registerValidateRules(): void
    {
        // TODO: Implement registerValidateRules() method.
    }

    function getMessages(): array
    {
        // TODO: Implement getMessages() method.
        return [];
    }

    /**
     * @param array $params
     * @return MessageBag|object
     * @throws ValidationException
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     */
    public static function addVersionValidate(array $params)
    {
        return self::getInstance()->validate([
            "clientId",
            "version",
            "versionChangeContent",
            "packageUrl",
            "packagingToolVersion",
            "releaseVersion"
        ], $params, [
            "clientId"             => [
                "required",
                "int",
                Rule::exists((new ClientsModel())->getTable(), "id")->where("isDeleted", 0)
            ],
            "version"              => [
                "required",
                "string",
                "regex:/^[1-9][\d]*+[\.\d+]{1,10}$/",
                Rule::unique((new ClientsVersionModel())->getTable(), "version")
                    ->where("clientId", $params["clientId"] ?? 0)->where("isDeleted", 0),
                function ($attribute, $value, $fail) use ($params) {
                    $maxVersion = ClientsVersionModel::query()
                                                     ->where("clientId", $params["clientId"] ?? 0)
                                                     ->max("version");
                    if (version_compare($value, $maxVersion) <= 0) {
                        $fail("选定版本不能低于已发布的版本，请重新填写");
                    }
                }
            ],
            "versionChangeContent" => "required|string|min:1|max:255",
            "packageUrl"           => "required|url",
            "packagingToolVersion" => [
                "required",
                "string",
                "regex:/^[1-9][\d]*+[\.\d+]{1,10}$/"
            ],
            "releaseVersion"       => "int|in:0,1"
        ]);
    }

    /**
     * @param array $params
     * @return MessageBag|object
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     * @throws ValidationException
     */
    public static function updateVersionValidate(array $params)
    {
        return self::getInstance()->validate([
            "versionId",
            "versionChangeContent",
            "packageUrl",
            "packagingToolVersion",
            "releaseVersion"
        ], $params, [
            "versionId"            => [
                "required",
                "int",
                function ($attribute, $value, $fail) use ($params) {
                    $exists = ClientsVersionReleaseHistoryModel::query()
                                                               ->where("versionId", $value)
                                                               ->exists();
                    if ($exists) {
                        $fail("选定版本已存在发布历史，不支持修改相关信息");
                    }
                }
            ],
            "clientId"             => [
                "required",
                "int",
                Rule::exists((new ClientsModel())->getTable(), "id")->where("isDeleted", 0)
            ],
            "version"              => [
                "required",
                "string",
                "regex:/^[1-9][\d]*+[\.\d+]{1,10}$/",
                Rule::unique((new ClientsVersionModel())->getTable(), "version")
                    ->where(function ($query) use ($params) {
                        $query->where("id", "<>", $params["versionId"] ?? 0);
                    })->where("clientId", $params["clientId"] ?? 0)
                    ->where("isDeleted", 0),
                function ($attribute, $value, $fail) use ($params) {
                    $maxVersion = ClientsVersionModel::query()
                                                     ->where(function ($query) use ($params) {
                                                         $query->where("id", "<>", $params["versionId"] ?? 0);
                                                     })
                                                     ->where("clientId", $params["clientId"] ?? 0)
                                                     ->max("version");
                    if (version_compare($value, $maxVersion) <= 0) {
                        $fail("选定版本不能低于已发布的版本，请重新填写");
                    }
                }
            ],
            "versionChangeContent" => "required|string|min:1|max:255",
            "packageUrl"           => "required|url",
            "packagingToolVersion" => [
                "required",
                "string",
                "regex:/^[1-9][\d]*+[\.\d+]{1,10}$/"
            ],
            "releaseVersion"       => "int|in:0,1"
        ]);
    }

    /**
     * @param array $params
     * @return MessageBag|object
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     * @throws ValidationException
     */
    public static function deleteVersionValidate(array $params)
    {
        return self::getInstance()->validate([
            "versionId"
        ], $params, [
            "versionId" => [
                "required",
                "int",
                function ($attribute, $value, $fail) use ($params) {
                    $exists = ClientsVersionReleaseHistoryModel::query()
                                                               ->where("versionId", $value)
                                                               ->exists();
                    if ($exists) {
                        $fail("选定版本已存在发布历史，不支持删除当前版本信息");
                    }
                }
            ]
        ]);
    }

    /**
     * @param array $params
     * @return MessageBag|object
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     * @throws ValidationException
     */
    public static function releaseVersionValidate(array $params)
    {
        return self::getInstance()->validate([
            "clientId",
            "versionId"
        ], $params, [
            "clientId"  => [
                "required",
                "int",
                Rule::exists((new ClientsModel())->getTable(), "id")->where("isDeleted", 0),
                function ($attribute, $value, $fail) use ($params) {
                    $exists = ClientsModel::query()
                                          ->where("id", $value)
                                          ->where("versionId", $params["versionId"] ?? 0)
                                          ->exists();
                    if ($exists) {
                        $fail("应用已发布当前版本为最新版本，请勿重复发布");
                    }
                }
            ],
            "versionId" => [
                "required",
                "int",
                Rule::exists((new ClientsVersionModel())->getTable(), "id")
                    ->where("clientId", $params["clientId"] ?? 0)
                    ->where("isDeleted", 0)
            ],
        ]);
    }


    /**
     * @param array $params
     * @return MessageBag|object
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     * @throws ValidationException
     */
    public static function listValidate(array $params)
    {
        return self::getInstance()->validate([
            "clientId",
            "pageNum",
            "pageSize"
        ], $params, [
            "pageNum"  => "int|min:1",
            "pageSize" => "int|min:1",
            "clientId" => [
                "required",
                "int",
                Rule::exists((new ClientsModel())->getTable(), "id")->where("isDeleted", 0)
            ]
        ]);
    }
}
